package com.wwdx.controller.rest;

import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
import com.wwdx.controller.base.BaseController;
import com.wwdx.model.WwdxBookModel;
import com.wwdx.model.dto.PurchaseCordDTO;
import com.wwdx.model.dto.ResultBean;
import com.wwdx.utils.ConstantHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;
import java.util.Map;
/**
 * @author liguoliang
 */
@RestController
@RequestMapping("user")
@Api(tags = "用户接口")
public class RestUserController extends BaseController{

    /**
     * 用户已购买记录
     */
    @RequestMapping("/purchase")
    @ApiOperation(value = "用户已购列表",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "令牌",required = true),
            @ApiImplicitParam(name = "sign",value = "签名",required = true)
    })
    public ResultBean purchase(String token, String sign){
        ResultBean resultBean = super.validSignTokenResult(super.getSortedMap(token,sign));
        if (resultBean.getStatus() == 1){
            return resultBean;
        }
        String unionId = (String) resultBean.getData();
        return renderSuccess(wwdxOrderService.bookshelfList(unionId));
    }

    /**
     * 撰稿人信息
     * @param userid 撰稿人或音频转述师ID
     * @param unionid 用户业务ID
     * @param page 分页参数
     * @param rows 分页参数
     */
    @RequestMapping("/userInfo")
    @ApiOperation(value = "撰稿人或音频转述师信息",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid",value = "撰稿人或音频转述师ID",required = true),
            @ApiImplicitParam(name = "page",value = "分页参数",defaultValue = "1"),
            @ApiImplicitParam(name = "rows",value = "分页参数",defaultValue = "10"),
            @ApiImplicitParam(name = "unionid",value = "用户业务ID")
    })
    public ResultBean userInfo(Long userid, String unionid,
                               @RequestParam(value = "page",defaultValue = "1") Integer page,
                               @RequestParam(value = "rows",defaultValue = "10") Integer rows){

        return renderSuccess(wwdxCopywriterService.getUserInfo(userid,unionid,page,rows));
    }

    /**
     * 添加书架
     * @param unionid 用户业务ID
     * @param id 产品ID （书籍 | 主题包）
     */
    @RequestMapping("/bookshelf/add")
    @ApiOperation(value = "添加书架",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "产品ID",required = true),
            @ApiImplicitParam(name = "unionid",value = "用户业务ID",required = true)
    })
    public ResultBean bookshelfadd(String unionid, String id){
        Preconditions.checkArgument(StringUtils.isNotBlank(unionid),"unionid is null");
        //书籍
        WwdxBookModel wwdxBookModel = wwdxBookService.getByBookId(Long.valueOf(id));
        if (wwdxBookModel != null){
            PurchaseCordDTO dto = new PurchaseCordDTO();
            dto.setIndate(new DateTime().toString(ConstantHolder.Constant.LOCAL_DATE_TIME_PATTERN));
            dto.setIsbookshelf(1);
            Integer type = wwdxBookModel.getType();
            dto.setType(type == 0 ? "1" : "2");
            dto.setThemeid(String.valueOf(wwdxBookModel.getBookid()));
            dto.setReadingname(wwdxBookModel.getBookname());
            dto.setReadingimg(wwdxBookModel.getBookimg());
            dto.setBookrecommend(wwdxBookModel.getBookrecommend());
            dto.setVideoduration(wwdxBookModel.getVideoduration());
            dto.setVideoauthorid(wwdxBookModel.getVideoauthorid());
            dto.setCopywriter(wwdxBookModel.getCopywriter());
            dto.setBookchapter_list(wwdxBookChapterService.findBookChapterList(Long.valueOf(id)));
            String key = String.format(ConstantHolder.USER_BOOKSHELF_MAP,unionid);
            hashOperations.put(key,id, dto);
        }
        return renderSuccess();
    }

    /**
     * 移除书架
     * @param unionid 用户业务ID
     * @param id 产品ID （书籍 | 主题包）
     */
    @RequestMapping("/bookshelf/del")
    @ApiOperation(value = "移除书架",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "产品ID",required = true),
            @ApiImplicitParam(name = "unionid",value = "用户业务ID",required = true)
    })
    public ResultBean bookshelfdel(String unionid, String id){
        String key = String.format(ConstantHolder.USER_BOOKSHELF_MAP,unionid);
        hashOperations.delete(key,id);
        return renderSuccess();
    }

    /**
     * 查询用户观看书籍数与观看时长
     * @param unionid 用户业务ID
     */
    @RequestMapping("/duration/get")
    @ApiOperation(value = "查询用户观看书籍数与观看时长",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "unionid",value = "用户业务ID",required = true)
    })
    public ResultBean durationget(String unionid){
        Preconditions.checkArgument(StringUtils.isNotBlank(unionid),"durationget unionid is null");
        Map<String,Object> map = Maps.newHashMapWithExpectedSize(2);
        String key = String.format(ConstantHolder.USER_DURATION_LIST,unionid);
        map.put("duration",listOperations.size(key));
        map.put("booknum",new HashSet<>(listOperations.range(key,0,-1)).size());
        return renderSuccess(map);
    }

    /**
     * 保存用户观看书籍数与观看时长
     * @param unionid 用户业务ID
     * @param bookid 书籍ID
     */
    @RequestMapping("/duration/add")
    @ApiOperation(value = "保存用户观看书籍数与观看时长",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "unionid",value = "用户业务ID",required = true),
            @ApiImplicitParam(name = "bookid",value = "书籍ID",required = true)
    })
    public void durationadd(String unionid, Long bookid){
        Preconditions.checkArgument(StringUtils.isNotBlank(unionid),"durationadd unionid is null");
        Preconditions.checkArgument(bookid !=null,"bookid is null");
        String key = String.format(ConstantHolder.USER_DURATION_LIST,unionid);
        listOperations.leftPush(key,bookid.toString());
    }
}